// pages/service/service.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    active: 'all',
    orderList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      page: 1,
      active: 'all'
    })
    this.getList()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading()
    this.getOrderList(this.data.page, this.data.active)
    wx.hideNavigationBarLoading()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let page = this.data.page
    page = page + 1
    this.setData({
      page: page
    })
    this.getList(this.data.page, this.data.active)
  },

  /**
   * 
   */
  getList: function(page, type) {
    let that = this,
      orderList = that.data.orderList,
      list = [],
      data, k
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/refund-order/list?access_token=' + accessToken,
        data: {
          page: page,
          refund_order_type: type
        },
        success: res => {
          data = res.data.data
          if (res.data.status === 1) {
            if (page == 1) {
              that.setData({
                orderList: res.data.data
              },wx.hideLoading())
            } else {
              if (data.length != 0) {
                for (k in data) {
                  list.push(data[k])
                }
                that.setData({
                  orderList: list
                }, wx.hideLoading())
              }
            }
          } else {
            wx.hideLoading()
            wx.showToast({
              icon: 'none',
              title: res.data.msg,
            })
          }
        },
        fail: res => {
          wx.hideLoading()
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
          })
        }
      })
    })
  },

  tab: function(e) {
    let that = this,
      active = e.currentTarget.dataset.index
    this.setData({
      active: active
    })
    that.getList(1, that.data.active)
  }
})